<?php
/**
 * Copyright (c) 2007 PORTALIER Julien
 * All rights reserved.
 */

class GenericFilerController extends AppController
{
	public $_checkAccess = array(
		'index'  => 'read',
		'upload' => 'write',
		'delete' => 'delete',
	);
	public    $uses   = null;
	public    $layout = 'basic';
	protected $root;
	
	public    $_templates  = 'filer';
	public    $full_domain = DOMAIN;
	
	# internals
	
	function beforeAction()
	{
		parent::beforeAction();
		
		if (array_key_exists($this->action, $this->_checkAccess))
			$this->Acl->checkAccess("{$this->viewPath}.{$this->action}", $this->_checkAccess[$this->action]);
		
		# chroot
		if (empty($this->root))
			$this->root = WWW_ROOT.'files'.DS;
		
		# base url
		if (empty($this->baseURL))
			$this->baseURL = empty(Load::$plugin) ? '/'.$this->viewPath : '/'.Load::$plugin.'/'.$this->viewPath;
		
		# file manipulator
		Load::libs('files');
		$this->Files = new Files($this->root);
		
		# the path is all URI's segments
		$this->params['args'] = array(implode('/', $this->params['args']));
	}
	
	function render($render=null)
	{
		$this->set('baseURL', $this->baseURL);
		$this->set('path', ($this->path == DS) ? null : $this->path);
		$this->set('_path', "/files".(($this->path == DS) ? '' : "/{$this->path}"));
		
		$this->set('listing', $this->Files->ls($this->path));
		if ($this->action != 'index')
			$this->set('partial', empty($render) ? $this->action : $render);
		parent::render('index', HTTP::isAjax() ? 'ajax' : 'basic');
	}
	
	// actions
	
	function index($path)
	{
		$this->path = $path.DS;
		if (!$this->Files->exists($this->path))
			Error::show(404);
		
		$this->pageTitle = sprintf(_('Index of %s'), $this->path);
	}
	
	function stat($filename)
	{
		$this->path = dirname($filename);
		$this->path = ($this->path == '.') ? '' : $this->path.DS;
		$this->data = $this->Files->ls($filename);
		$this->set('filename', basename($filename));
		
		$this->pageTitle = sprintf(_('Statistics for %s'), $filename);
		
		switch($this->data['mimetype'])
		{
			case 'image/jpeg': case 'image/png': case 'image/gif':
				$this->render('stat_image'); break;
#			case 'image/svg+xml':
#				$this->render('stat_svg'); break;
			case 'video/x-flv':
				$this->render('stat_video'); break;
			case 'audio/mpeg':
				$this->render('stat_audio'); break;
			case 'application/x-shockwave-flash':
				$this->render('stat_flash'); break;
		}
	}
	
	function mkdir($path)
	{
		$this->path = $path.DS;
		if (isset($this->data['new_folder']))
		{
			if ($this->Files->mkdir($this->path.$this->data['new_folder']))
				HTTP::redirect("{$this->baseURL}/index/{$this->path}{$this->data['new_folder']}");
			$this->flash(_('An error occured.'), HTTP::getReferer(), 'error');
		}
		$this->pageTitle = sprintf(_('New folder in %s'), "/$path");
	}
	
	function upload($path)
	{
		$this->path = $path.DS;
		
		if (HTTP::isPost())
		{
			Load::libs('file_upload');
			$FileUpload = new FileUpload;
			$path_to    = $this->Files->root.($this->path == '/' ? '' : $this->path);
			
			try
			{
				$FileUpload->save('Filedata', $path_to);
				
				if (!empty($path))
					$path .= '/';
				HTTP::redirect("{$this->baseURL}/stat/$path".urlencode($FileUpload->getFilename('Filedata')));
			}
			catch(UploadException $e) {
				$this->Session->setFlash($e->getMessage(), 'error');
			}
		}
		$this->pageTitle = _('Upload files from your computer');
	}
	
	function delete($path)
	{
		$mesg = $this->Files->rmdir($path) ?
			_('File or directory deleted.') : _('An error occured.');
		HTTP::redirect("{$this->baseURL}/index/".dirname($path));
	}
	
	function format_size($size)
	{
		if ($size < 1024)
			return sprintf(_('%d Bytes'), $size);
		if ($size < 1024 * 1024)
			return sprintf(_('%.2f KBytes'), $size / 1024);
		if ($size < 1024 * 1024 * 1024)
			return sprintf(_('%.2f MBytes'), $size / 1024 / 1024);
		if ($size < 1024 * 1024 * 1024 * 1024)
			return sprintf(_('%.2f GBytes'), $size / 1024 / 1024);
	}
}
?>
